www.gusucode.com > VC 文件二进制代码阅读器-源码程序 > VC 文件二进制代码阅读器-源码程序/code/BinaryView/BinaryView.cpp

    //Download by http://www.NewXing.com
#include "stdafx.h"
#include "Tables.h"

//**********	Functions	**********

void MenuMain(int FirstPos);

//*****	CallBack	*****
LRESULT CALLBACK WndProc(HWND ,UINT ,WPARAM ,LPARAM );

//*****	File	*****
bool Open_File(LPSTR FileName);
void OpenFileBox();

//*****	Window	*****
void WinDim();

//*****	Text	*****
void cLines();
void MaxWidthOf(UINT *MaxW,char Text[10]);

//*****	ScrollBar	*****
void SetScrollBar();
void ScrollUp();
void ScrollDown();
void PageUp();
void PageDown();
void Scrolling(bool End);


//**********	Vars	**********

//*****	Window	*****
WNDCLASSEX	wc;
HWND		Main;
HDC			Hdc;
int			wWidth,
			wHeight;
int			cWidth,
			cHeight;

HPEN		LtGrayPen,ex_pen;

char TempChar[MAX_PATH];

//*****	Text	*****
UINT FirstLine;
UINT tHeight;
UINT Lines;

//*****	Scroll Bar	*****
SCROLLINFO si;

//*****	File	*****
BYTE	*B;
DWORD	FileSize;



int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
	wc.cbSize=sizeof(wc);
	wc.hInstance=hInstance;
	wc.lpszClassName="BinaryView";
	wc.lpfnWndProc=WndProc;
	wc.style=CS_HREDRAW | CS_VREDRAW;
	wc.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(ICON_MAIN));
	wc.hIconSm=LoadIcon(hInstance,MAKEINTRESOURCE(ICON_MAIN));
	wc.hCursor=LoadCursor(NULL,IDC_ARROW);
	wc.cbClsExtra=0;
	wc.cbWndExtra=0;
	wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName=NULL;
	
	if(!RegisterClassEx(&wc))return 0;

	wWidth=200;
	wHeight=150;

	Main=CreateWindow("BinaryView","Binary View",WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_VSCROLL | WS_SIZEBOX,CW_USEDEFAULT,CW_USEDEFAULT,wWidth,wHeight,NULL,NULL,hInstance,NULL);

	for(UINT ascii=0;ascii<256;ascii++)
	{
		sprintf(&AsciiTable[ascii][0],"%c",ascii);
		AsciiTable[ascii][1]='\0';
	}

	if(strlen(lpCmdLine)==0)
	{
		OpenFileBox();
		if(strlen(TempChar)==0)return 0;
		if(!Open_File(TempChar))return 0;
	}
	else
	{
		if(!Open_File(lpCmdLine))return 0;
		GetLongPathName(lpCmdLine,TempChar,MAX_PATH);
	}

	SetWindowText(Main,TempChar);

	WinDim();

	FirstLine=0;

	Hdc=GetDC(Main);
	TEXTMETRIC tm;
	GetTextMetrics(Hdc,&tm);
	tHeight=tm.tmHeight+tm.tmExternalLeading;

	cLines();

	SetScrollBar();

	LtGrayPen=CreatePen(PS_SOLID,1,RGB(200,200,200));

	ShowWindow(Main,nCmdShow);
	UpdateWindow(Main);

	MSG msg;
	ZeroMemory(&msg,sizeof(msg));
	while(msg.message!=WM_QUIT)
	{
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			//CODE
		}
	}

	SelectObject(Hdc,ex_pen);
	DeleteObject(LtGrayPen);

	ReleaseDC(Main,Hdc);

	UnregisterClass("BinaryView",wc.hInstance);

	return 0;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC			hdc;
	UINT		i;
	UINT		TempMax;
	UINT		MaxW;
	UINT		Left;
	char		C[8];

	switch(msg)
	{
	case WM_RBUTTONUP:
		MenuMain((HIWORD(lParam)/tHeight)+FirstLine);
		break;
	case WM_SIZE:
		if(wParam==SIZE_MINIMIZED)break;
		cWidth = LOWORD(lParam);
		cHeight = HIWORD(lParam);
		cLines();
		SetScrollBar();
		break;
	case WM_VSCROLL:
		switch((int)LOWORD(wParam))
		{
		case SB_LINEDOWN:
			ScrollDown();
			return 0;
		case SB_LINEUP:
			ScrollUp();
			return 0;
		case SB_PAGEDOWN:
			PageDown();
			return 0;
		case SB_PAGEUP:
			PageUp();
			return 0;
		case SB_THUMBTRACK:
			Scrolling(false);
			return 0;
		case SB_THUMBPOSITION:
			Scrolling(true);
			return 0;
		}
		break;
	case WM_PAINT:
		if((FirstLine+Lines)>FileSize)
			TempMax=FileSize-FirstLine;
		else
			TempMax=FirstLine+Lines;

		hdc=BeginPaint(hWnd,&ps);

		ex_pen=(HPEN)SelectObject(hdc,LtGrayPen);
		SetTextColor(hdc,RGB(0,0,255));

		MaxW=0;
		for(i=FirstLine;i<TempMax;i++)
		{
			itoa(i,C,10);
			TextOut(hdc,0,(i-FirstLine)*tHeight,C,strlen(C));
			MaxWidthOf(&MaxW,C);
		}
		MaxW+=2;
		MoveToEx(hdc,MaxW,0,NULL);
		LineTo(hdc,MaxW,cHeight);

		SetTextColor(hdc,RGB(0,0,0));

		Left=(MaxW+2);
		MaxW=0;
		for(i=FirstLine;i<TempMax;i++)
		{
			TextOut(hdc,Left,(i-FirstLine)*tHeight,&BytesTable[B[i]][0],BytesLen[B[i]]);
			MaxWidthOf(&MaxW,&BytesTable[B[i]][0]);
		}
		MaxW+=(Left+2);
		MoveToEx(hdc,MaxW,0,NULL);
		LineTo(hdc,MaxW,cHeight);

		SetTextColor(hdc,RGB(0,128,0));

		Left=(MaxW+2);
		MaxW=0;
		for(i=FirstLine;i<TempMax;i++)
		{
			TextOut(hdc,Left,(i-FirstLine)*tHeight,&AsciiTable[B[i]][0],1);
			MaxWidthOf(&MaxW,&AsciiTable[B[i]][0]);
		}
		MaxW+=(Left+2);
		MoveToEx(hdc,MaxW,0,NULL);
		LineTo(hdc,MaxW,cHeight);

		SetTextColor(hdc,RGB(128,0,0));

		Left=(MaxW+2);
		MaxW=0;
		for(i=FirstLine;i<TempMax;i++)
		{
			TextOut(hdc,Left,(i-FirstLine)*tHeight,&BinaryTable[B[i]][0],8);
			MaxWidthOf(&MaxW,&BinaryTable[B[i]][0]);
		}
		MaxW+=(Left+2);
		MoveToEx(hdc,MaxW,0,NULL);
		LineTo(hdc,MaxW,cHeight);

		SetTextColor(hdc,RGB(255,0,255));

		Left=(MaxW+2);
		MaxW=0;
		for(i=FirstLine;i<TempMax;i++)
		{
			TextOut(hdc,Left,(i-FirstLine)*tHeight,&HexTable[B[i]][0],2);
			MaxWidthOf(&MaxW,&HexTable[B[i]][0]);

			MoveToEx(hdc,0,(i-FirstLine)*tHeight,NULL);
			LineTo(hdc,cWidth,(i-FirstLine)*tHeight);
		}
		MaxW+=(Left+2);
		MoveToEx(hdc,MaxW,0,NULL);
		LineTo(hdc,MaxW,cHeight);

		SelectObject(hdc,ex_pen);

		EndPaint(hWnd,&ps);
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
	}
	return DefWindowProc(hWnd,msg,wParam,lParam);
}

bool Open_File(LPSTR FileName)
{
	DWORD BytesWritten;

	HANDLE f=CreateFile(FileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL);
	if(f==INVALID_HANDLE_VALUE)return false;

	FileSize=GetFileSize(f,NULL);
	B=new BYTE[FileSize];
	ReadFile(f,B,FileSize,&BytesWritten,NULL);
	CloseHandle(f);

	return true;
}

void SetScrollBar()
{
	si.cbSize=sizeof(si);
	si.fMask=SIF_RANGE | SIF_PAGE;
	si.nMin=0;
	si.nMax=FileSize-Lines;
	si.nPage=1;

	SetScrollInfo(Main,SB_VERT,&si,TRUE);
}

void ScrollUp()
{
	if(FirstLine==0)return;

	FirstLine--;
	SetScrollPos(Main,SB_VERT,FirstLine,TRUE);
	InvalidateRect(Main,NULL,TRUE);
}

void ScrollDown()
{
	if(FirstLine==(UINT)si.nMax)return;

	FirstLine++;
	SetScrollPos(Main,SB_VERT,FirstLine,TRUE);
	InvalidateRect(Main,NULL,TRUE);
}

void PageUp()
{
	if(FirstLine<Lines)
		FirstLine=0;
	else
		FirstLine-=Lines;

	SetScrollPos(Main,SB_VERT,FirstLine,TRUE);
	InvalidateRect(Main,NULL,TRUE);
}

void PageDown()
{
	FirstLine+=Lines;

	if(FirstLine>(UINT)si.nMax)FirstLine=si.nMax;

	SetScrollPos(Main,SB_VERT,FirstLine,TRUE);
	InvalidateRect(Main,NULL,TRUE);
}

void Scrolling(bool End)
{
	SCROLLINFO tsi;

	tsi.cbSize=sizeof(tsi);
	tsi.fMask=SIF_TRACKPOS;
	GetScrollInfo(Main,SB_VERT,&tsi);

	FirstLine=tsi.nTrackPos;
	if(End)
		SetScrollPos(Main,SB_VERT,FirstLine,TRUE);

	InvalidateRect(Main,NULL,TRUE);
}

void WinDim()
{
	RECT TempRect;
	GetClientRect(Main,&TempRect);
	cWidth=TempRect.right;
	cHeight=TempRect.bottom;
}

void cLines()
{
	Lines=cHeight/tHeight;
}

void MaxWidthOf(UINT *MaxW,char Text[10])
{
	SIZE Size;
	GetTextExtentPoint32(Hdc,Text,strlen(Text),&Size);
	if((UINT)Size.cx>*MaxW)*MaxW=Size.cx;
}

void OpenFileBox()
{
	OPENFILENAME ofn;

	ZeroMemory(&ofn,sizeof(ofn));

	ofn.lStructSize=sizeof(ofn);
	ofn.hwndOwner=Main;
	ofn.lpstrFilter="All Files)*.*)\0*.*\0\0";
	ofn.lpstrCustomFilter=NULL;
	ofn.nFilterIndex=0;

	TempChar[0]='\0';
	ofn.lpstrFile=TempChar;

	ofn.nMaxFile=MAX_PATH;
	ofn.lpstrFileTitle=NULL;
	ofn.lpstrInitialDir=NULL;
	ofn.lpstrTitle="Open a file for Binary View\0";
	ofn.Flags=OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_LONGNAMES | OFN_PATHMUSTEXIST;
	ofn.lpstrDefExt=NULL;
	
	if(GetOpenFileName(&ofn)==0)return;

	strcpy(TempChar,ofn.lpstrFile);
}

void MenuMain(int FirstByte)
{
	char	DecVal16[99],
		DecVal32[99],
		HexVal16[99],
		HexVal32[99];

	int Value16=MAKEWORD(B[FirstByte],B[FirstByte+1]);
	int Value32=MAKELONG(MAKEWORD(B[FirstByte],B[FirstByte+1]),MAKEWORD(B[FirstByte+2],B[FirstByte+3]));

	itoa(Value16,DecVal16,10);
	itoa(Value32,DecVal32,10);

	itoa(Value16,HexVal16,16);
	itoa(Value32,HexVal32,16);

	MENUITEMINFO sep;
	MENUITEMINFO mii;

	sep.cbSize=sizeof(sep);
	sep.fMask=MIIM_TYPE;
	sep.fType=MFT_SEPARATOR;
	
	mii.cbSize=sizeof(mii);
	mii.fMask=MIIM_TYPE | MIIM_SUBMENU;
	mii.fType=MFT_STRING;

	HMENU Menu=CreatePopupMenu();
	HMENU v16=CreatePopupMenu();
	HMENU v32=CreatePopupMenu();

	mii.hSubMenu=v16;
	mii.dwTypeData="16 bit value";
	mii.cch=strlen(mii.dwTypeData);
	
	InsertMenuItem(Menu,0,TRUE,&mii);

	mii.hSubMenu=v32;
	mii.dwTypeData="32 bit value";
	mii.cch=strlen(mii.dwTypeData);
	
	InsertMenuItem(Menu,1,TRUE,&mii);

	int id=0xffff;

	mii.fMask=MIIM_TYPE | MIIM_ID;

	mii.wID=id;

	strrev(DecVal16);
	strcat(DecVal16," : ceD");
	strrev(DecVal16);

	mii.dwTypeData=DecVal16;
	mii.cch=strlen(mii.dwTypeData);

	InsertMenuItem(v16,0,TRUE,&mii);
	id--;

	mii.wID=id;

	strrev(HexVal16);
	strcat(HexVal16,"x0 : xeH");
	strrev(HexVal16);

	mii.dwTypeData=HexVal16;
	mii.cch=strlen(mii.dwTypeData);

	InsertMenuItem(v16,1,TRUE,&mii);
	id--;

	mii.wID=id;

	strrev(DecVal32);
	strcat(DecVal32," : ceD");
	strrev(DecVal32);

	mii.dwTypeData=DecVal32;
	mii.cch=strlen(mii.dwTypeData);

	InsertMenuItem(v32,0,TRUE,&mii);
	id--;

	mii.wID=id;

	strrev(HexVal32);
	strcat(HexVal32,"x0 : xeH");
	strrev(HexVal32);

	mii.dwTypeData=HexVal32;
	mii.cch=strlen(mii.dwTypeData);

	InsertMenuItem(v32,1,TRUE,&mii);
	id--;

	TPMPARAMS tpmp;
	POINT cp;

	tpmp.cbSize=sizeof(tpmp);
	SetRectEmpty(&tpmp.rcExclude);

	GetCursorPos(&cp);

	UINT RetVal=TrackPopupMenuEx(Menu,TPM_TOPALIGN | TPM_LEFTALIGN | TPM_NONOTIFY | TPM_RETURNCMD | TPM_LEFTBUTTON,cp.x,cp.y,Main,&tpmp);

	DestroyMenu(v16);
	DestroyMenu(v32);
	DestroyMenu(Menu);

}